﻿package cn.flashk.image
{
    import flash.display.*;
    import flash.geom.*;

    public class GridBitmap extends Bitmap
    {
        public var isHorizontal:Boolean = true;
        protected var sourceBD:BitmapData;
        protected var gw:uint;
        protected var gh:uint;
        protected var bd:BitmapData;
        protected var lastViewFrame:uint;
        protected var lastX:uint;
        protected var lastY:uint;

        public function GridBitmap()
        {
            return;
        }// end function

        public function set sourceBitmapData(param1:BitmapData) : void
        {
            sourceBD = param1;
            return;
        }// end function

        public function get gridWidth() : uint
        {
            return Math.round(sourceBD.width / gw);
        }// end function

        public function get gridHeight() : uint
        {
            return Math.round(sourceBD.height / gh);
        }// end function

        public function get totalFrames() : uint
        {
            return gridWidth * gridHeight;
        }// end function

        public function setGridSize(param1:uint, param2:uint) : void
        {
            gw = param1;
            gh = param2;
            if (bd != null)
            {
                bd.dispose();
            }
            bd = new BitmapData(gw, gh, true, 0);
            this.bitmapData = bd;
            return;
        }// end function

        public function viewByXY(param1:uint, param2:uint) : void
        {
            bd.copyPixels(sourceBD, new Rectangle((param1 - 1) * gw, (param2 - 1) * gh, gw, gh), new Point(0, 0));
            lastX = param1;
            lastY = param2;
            return;
        }// end function

        public function viewByFrame(param1:uint) : void
        {
            var _loc_2:uint = 0;
            var _loc_3:uint = 0;
            var _loc_4:uint = 0;
            if (param1 == 0)
            {
                param1 = 1;
            }
            if (lastViewFrame == param1)
            {
                return;
            }
            if (isHorizontal == true)
            {
                _loc_2 = Math.round(sourceBD.width / gw);
                _loc_3 = param1 % _loc_2;
                if (_loc_3 == 0)
                {
                    _loc_3 = _loc_2;
                }
                _loc_4 = uint((param1 - 1) / _loc_2) + 1;
            }
            else
            {
                _loc_2 = Math.round(sourceBD.height / gh);
                _loc_4 = param1 % _loc_2;
                if (_loc_4 == 0)
                {
                    _loc_4 = _loc_2;
                }
                _loc_3 = uint((param1 - 1) / _loc_2) + 1;
            }
            viewByXY(_loc_3, _loc_4);
            lastViewFrame = param1;
            return;
        }// end function

        public function update() : void
        {
            viewByXY(lastX, lastY);
            return;
        }// end function

    }
}
